<?php
class GroupsController extends Controller
{
	protected $adapter = null;
	
	public function init()
	{
		$this->adapter = V::getAdapter();
	}
	
	public function addAction()
	{
		$lastId = $this->adapter->fields(array('group_name' => 'super admin'))->table('user_groups')->insert();
		echo $this->adapter->getSql(), '<br/>Last Insert Id：', $lastId, '<br/>';
		$lastId = $this->adapter->fields(array('group_name' => 'admin'))->table('user_groups', 'vo_')->insert();
		echo $this->adapter->getSql(), '<br/>Last Insert Id：', $lastId, '<br/>';
		$lastId = $this->adapter->fields(array('group_name' => 'member'))->table(array('user_groups'))->insert();
		echo $this->adapter->getSql(), '<br/>Last Insert Id：', $lastId, '<br/>';
		$lastId = $this->adapter->fields(array('group_name' => 'test'))->table(array('user_groups'), 'vo_')->insert();
		echo $this->adapter->getSql(), '<br/>Last Insert Id：', $lastId, '<br/>';
		
		exit;
	}
	
	public function userlistAction()
	{
		$rows = $this->adapter->fields(array('group_id', 'group_name'), 'g')
		->fields(array('user_id', 'user_loginname', 'group_id', 'user_nicename', 'dt_registered', 'dt_last_login'), 'u')
		->table(array('g' => 'user_groups', 'u' => 'users'))
		->where(array('u.group_id = g.group_id AND g.group_id = ? AND u.user_status = ?', 2, 'y'))
		->limit(5)->order('dt_registered')->select();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($rows);
		
		exit;
	}
	
	public function assocAction()
	{
		$rows = $this->adapter
		->fields(array('user_id', 'user_loginname', 'group_id', 'user_nicename', 'dt_registered', 'dt_last_login'), 'u')
		->fields(array('group_id', 'group_name'), 'g')
		->table(array('g' => 'user_groups'), 'vo_')
		->table(array('u' => 'users'), 'vo_')
		->where(array('u.group_id = g.group_id AND g.group_id = ? AND u.user_status = ?', 1, 'y'))
		->limit(10)->order('dt_registered')->getAssoc();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($this->adapter->getParams());
		print_r($rows);
		exit;
	}
	
	public function join1Action()
	{
		$rows = $this->adapter->fields(array('group_id', 'group_name'), 'g')
		->fields(array('user_id', 'user_loginname', 'group_id', 'user_nicename', 'dt_registered', 'dt_last_login'), 'u')
		->join(array('g' => 'user_groups', 'u' => 'users'), 'u.group_id = g.group_id')
		->where(array('g.group_id = ? AND u.user_status = ?', 2, 'y'))
		->limit(5)->order('dt_registered')->select();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($rows);
		
		exit;
	}
	
	public function join2Action()
	{
		$rows = $this->adapter->fields(array('group_id', 'group_name'), 'g')
		->fields(array('user_id', 'user_loginname', 'group_id', 'user_nicename', 'dt_registered', 'dt_last_login'), 'u')
		->join(array('g' => 'user_groups', 'u' => 'users'), 'u.group_id = g.group_id', 'vo_')
		->where(array('g.group_id = ? AND u.user_status = ?', 2, 'y'))
		->limit(5)->order('dt_registered')->select();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($rows);
		
		exit;
	}
	
	public function join3Action()
	{
		$rows = $this->adapter->fields(array('group_id', 'group_name'), 'g')
		->fields(array('user_id', 'user_loginname', 'group_id', 'user_nicename', 'dt_registered', 'dt_last_login'), 'u')
		->join(array('g' => 'user_groups'), '', 'vo_')
		->join(array('u' => 'users'), 'u.group_id = g.group_id', 'vo_')
		->where(array('g.group_id = ? AND u.user_status = ?', 2, 'y'))
		->limit(5)->order('dt_registered')->select();
		
		echo '<pre>';
		
		echo $this->adapter->getSql(), '<br/>';
		print_r($rows);
		
		exit;
	}
} 
